#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
调试装饰器问题
"""

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from backend.app import create_app
from backend.api.netdisk import download_file_by_path
from backend.utils.auth import login_required

def debug_decorator():
    """调试装饰器"""
    print("=== 装饰器调试信息 ===")
    
    # 检查函数本身
    print(f"函数名: {download_file_by_path.__name__}")
    print(f"函数模块: {download_file_by_path.__module__}")
    print(f"函数ID: {id(download_file_by_path)}")
    
    # 检查装饰器链
    func = download_file_by_path
    level = 0
    print("\n装饰器链:")
    while hasattr(func, '__wrapped__'):
        level += 1
        print(f"  层级 {level}: {func.__name__} (ID: {id(func)})")
        func = func.__wrapped__
    print(f"  最终函数: {func.__name__} (ID: {id(func)})")
    
    # 检查装饰器函数
    print(f"\nlogin_required函数: {login_required}")
    print(f"login_required ID: {id(login_required)}")
    
    # 手动测试装饰器
    print("\n=== 手动测试装饰器 ===")
    
    @login_required
    def test_func():
        return "测试成功"
    
    print(f"测试函数: {test_func.__name__}")
    print(f"测试函数是否有__wrapped__: {hasattr(test_func, '__wrapped__')}")
    
    # 创建应用上下文进行测试
    app = create_app()
    with app.test_client() as client:
        with app.app_context():
            # 模拟请求上下文
            with client.application.test_request_context('/'):
                try:
                    result = test_func()
                    print(f"测试函数结果: {result}")
                except Exception as e:
                    print(f"测试函数异常: {str(e)}")
                    print(f"异常类型: {type(e)}")

if __name__ == "__main__":
    debug_decorator()